package com.game;

import java.util.*;

/**
 * 房间
 */
public class Room {

    private Integer id;//房间ID号
    private Integer personNum = 4;//人数，52张牌
    private Integer cardNum = 13;//卡牌数量

    private List<Integer> uids = new ArrayList<>();
    //三人选择15张,单A，单2，三K
    //三人选择16张，单2，三A
    private List<Card> cards = new ArrayList<>(52);
    
    public static void main(String[] args) {
        Room room = new Room();
        room.refreshSequence();
        room.handOut();

    }

    /**
     * 洗牌:刷新顺序
     */
    public void refreshSequence(){
        int allNum = personNum * cardNum;
        Random random = new Random();
        for(int id=1; id<=allNum; id++){
            cards.add(new Card(id));
        }
        for(int n = allNum; n>0; n--){
            //随机一个下标,当作是洗牌了
            int index = random.nextInt(n);
            cards.add(cards.remove(index));
        }
    }

    /**
     * 发牌
     */
    public void handOut(){
        Map<Integer,List<Card>> personCardMap = new HashMap<>();
        for(int num =0; num<cards.size(); num++){
            int key = num%personNum;
            List<Card> list = personCardMap.get(key);
            if(list==null){
                list = new ArrayList<>(cardNum);
                personCardMap.put(key,list);
            }
            list.add(cards.get(num));
        }
        for(int key: personCardMap.keySet()){
            List<Card> list = personCardMap.get(key);
            list.sort(Comparator.comparing(Card::getId).reversed());//倒序
        }
    }
    
}
